﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using VSSTQH.DataAccess;

namespace VSSTQH
{
    public partial class FrmVendor : Form
    {
        #region Ctors
        private FrmVendor()
        {
            InitializeComponent();

            Initialize();

        }
        #endregion

        #region Singleton Members
        private static volatile FrmVendor instance;
        private static object syncRoot = new Object();

        public static FrmVendor Instance
        {
            get
            {
                lock (syncRoot)
                {
                    if (instance == null)
                        instance = new FrmVendor();
                }

                return instance;
            }
        }

        protected override void OnClosed(EventArgs e)
        {
            instance = null;
            base.OnClosed(e);
        }
        #endregion

        #region Field Members
        IVendorDAO _dataAccess = new VendorDAO();
        IList<VendorModel> _vendorList;
        #endregion

        #region Method Members

        /// <summary>
        /// Initialize methods of the application
        /// </summary>
        void Initialize()
        {
            // Set Events
            this.btnReset.Click += new EventHandler(delegate { ClickButtonReset(); });
            this.btnSearch.Click += new EventHandler(delegate { ClickButtonSearch(); });
            this.btnNew.Click += new EventHandler(delegate { ClickButtonNew(); });
            this.btnEdit.Click += new EventHandler(delegate { ClickButtonEdit(); });
            this.btnDelete.Click += new EventHandler(delegate { ClickButtonDelete(); });
            this.btnClose.Click += new EventHandler(delegate { this.Close(); });
            this.gridEX1.DoubleClick += new EventHandler(delegate { ClickButtonEdit(); });

            // Show all vendors
            this.ClickButtonReset();
        }

        #endregion

        #region Action Members

        /// <summary>
        /// Click button reset
        /// </summary>
        private void ClickButtonReset()
        {
            this.edtVendorID.Text = String.Empty;
            this.edtVendorName.Text = String.Empty;
            this.edtTelephone.Text = String.Empty;
            this.edtAddress.Text = String.Empty;
            this.edtFaxNumber.Text = String.Empty;
            this.edtTaxCode.Text = String.Empty;

            ClickButtonSearch();

            this.edtVendorID.Focus();
        }

        /// <summary>
        /// Click button search
        /// </summary>
        private void ClickButtonSearch()
        {
            _dataAccess.SearchObject = new VendorModel();
            _dataAccess.SearchObject.VendorID = this.edtVendorID.Text;
            _dataAccess.SearchObject.VendorName = this.edtVendorName.Text;
            _dataAccess.SearchObject.Telephone = this.edtTelephone.Text;
            _dataAccess.SearchObject.Fax = this.edtFaxNumber.Text;
            _dataAccess.SearchObject.Address = this.edtAddress.Text;
            _dataAccess.SearchObject.TaxID = this.edtTaxCode.Text;

            _vendorList = _dataAccess.Search();

            string message = String.Empty;
            if (_vendorList == null || _vendorList.Count == 0)
                message = "Không tìm thấy thông tin.";
            else
                message = "Chọn cột cần group.";
            this.gridEX1.BuiltInTexts[Janus.Windows.GridEX.GridEXBuiltInText.GroupByBoxInfo] = message;

            this.gridEX1.DataSource = _vendorList;
        }

        /// <summary>
        /// Click button new
        /// </summary>
        private void ClickButtonNew()
        {
            int count = _vendorList.Count;
            // Show the updating staff instance
            FrmUpdateVendor instance = new FrmUpdateVendor();
            instance.Collection = _vendorList;
            instance.ShowDialog();
            if (count < _vendorList.Count)
            {
                // Refresh data
                this.gridEX1.Refetch();
            }
        }

        /// <summary>
        /// Click button edit
        /// </summary>
        private void ClickButtonEdit()
        {
            if (this.gridEX1.CurrentRow == null || this.gridEX1.CurrentRow.DataRow == null)
            {
                Common.ShowAlert("Vui lòng chọn dòng cần chỉnh sửa.");
                return;
            }

            // Edit employee information
            FrmUpdateVendor instance = new FrmUpdateVendor();
            instance.Vendor = this.gridEX1.CurrentRow.DataRow as VendorModel;
            DialogResult result = instance.ShowDialog();
            if (result == DialogResult.OK)
            {
                // Refresh data
                this.gridEX1.Refetch();
            }
        }

        /// <summary>
        /// Click button delete
        /// </summary>
        private void ClickButtonDelete()
        {
            if (this.gridEX1.CurrentRow == null)
            {
                Common.ShowAlert("Vui lòng chọn dòng cần xóa!");
                return;
            }

            VendorModel vendor = this.gridEX1.CurrentRow.DataRow as VendorModel;
            bool? result = Common.ShowYesNoQuestion("Bạn có chắc sẽ xóa thông tin này?");
            if (result.HasValue && result.Value)
            {
                string custID = vendor.VendorID;
                bool r = _dataAccess.Delete(vendor);

                if (r)
                {
                    this.gridEX1.CurrentRow.Delete();
                    this.gridEX1.Refetch();
                }
                else
                {
                    Common.ShowAlert("Xóa thông tin Vendor không thành công!");
                }
            }
        }

        #endregion
    }
}
